<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      title="查询"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="发生日期">
              <el-date-picker
                v-model="dateRange"
                size="small"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                style="width: 240px"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制造单号" prop="moeZzdh">
              <el-input
                v-model="queryParams.moeZzdh"
                placeholder="请输入制造单号"
                clearable
                style="width: 240px"
                size="small"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机台编号" prop="moeJtbh">
              <el-input
                v-model="queryParams.moeJtbh"
                placeholder="请输入制造单号"
                clearable
                style="width: 240px"
                size="small"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8" v-if="!query">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel(1)"
          size="small"
          >明细数据</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['repertory:InOutDetail:export']"
          @click="handleExcel(2)"
          size="small"
        >
          汇总数据
        </el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="制工单盈亏明细" name="first">
        <el-table
          border
          stripe
          id="inOutDetail"
          :height="tableHChild"
          :data="
            moneyMaterialProfitList.slice(
              (currentPage - 1) * pageSize,
              currentPage * pageSize
            )
          "
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column label="制造单号" align="center" prop="molZzdh" />
          <el-table-column label="类型" align="center" prop="molBzDesc" />
          <el-table-column label="物料代码" align="center" prop="molWldm" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column label="数量" align="center" prop="molXqsl" />
          <el-table-column label="单价" align="center" prop="molMoney" />
          <el-table-column label="总价" align="center" prop="molTotalMoney" />
        </el-table>
        <div>
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="pageSize"
            layout="total,prev,pager,next"
            :total="moneyMaterialProfitList.length"
          ></el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="制工单盈亏汇总" name="second">
        <el-table
          border
          stripe
          id="inOutTotal"
          :height="tableHChild"
          :data="moneyMaterialProfitTotalList"
        >
          <el-table-column label="#" align="center">
            <template #default="scope">
              {{ scope.$index + (currentPage - 1) * pageSize + 1 }}
            </template>
          </el-table-column>

          <el-table-column label="制造单号" align="center" prop="molZzdh" />
          <el-table-column label="物料代码" align="center" prop="molWldm" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column label="入库数量" align="center" prop="molXqsl" />
          <el-table-column label="单价" align="center" prop="molMoney" />
          <el-table-column label="总价" align="center" prop="molTotalMoney" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  exportInformation,
  getInformation,
} from "@/api/money/moneyMaterialProfit";

export default {
  name: "MoneyMaterialProfit",
  props: {
    query: null,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 表格高度
      tableHChild: document.body.clientHeight - 250,
      // 首选项
      activeTab: "first",
      // 明细 OR Total
      moneyMaterialProfitList: [],
      moneyMaterialProfitTotalList: [],
      // 显示搜索条件
      openSearch: false,
      currentPage: 1,
      pageSize: 20,

      // 建立日期时间范围
      dateRange: null,
      // 查询参数
      queryParams: {
        moeZzdh: null,
        moeJtbh: null,
        beginTime: null,
        endTime: null,
        index: null,
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  mounted() {
    if (this.query) this.getList();
  },
  watch: {
    query(newVal, oldVal) {
      this.getList();
    },
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.beginTime = this.moment(this.dateRange[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.endTime = this.moment(this.dateRange[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      if (this.query) this.queryParams = { ...this.queryParams, ...this.query };
      getInformation(this.queryParams).then((response) => {
        //明细
        this.moneyMaterialProfitList = response.data.details;
        //统计
        this.moneyMaterialProfitTotalList = response.data.total;
        this.loading = false;
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    handleSearch() {
      this.openSearch = true;
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.moneyMaterialProfitList = [];
      this.getList();
      this.openSearch = false;
    },
    //导出Excel
    handleExcel(a) {
      this.queryParams.index = a;
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportInformation(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
